// server.js
const WebSocket = require('ws');
const wss = new WebSocket.Server({ host: '0.0.0.0',port: 8090 });

wss.on('connection', function connection(ws) {
  ws.on('message', function incoming(message) {
    console.log('received: %s', message);

    // 广播消息到所有客户端
    wss.clients.forEach(function each(client) {
      if (client.readyState === WebSocket.OPEN) {
        client.send(message);
      }
    });
  });

  ws.send('connected to WebSocket server');
});

console.log('WebSocket server is running on ws://0.0.0.0:8090');